package com.iamting93.framework.web;

import com.iamting93.framework.config.MyConfiguration;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;

public class MyWebApplicationInitializer implements WebApplicationInitializer {

    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        AnnotationConfigWebApplicationContext ac = new AnnotationConfigWebApplicationContext();

        ac.register(MyConfiguration.class);
        ac.refresh();

        DispatcherServlet servlet = new DispatcherServlet(ac);

        ServletRegistration.Dynamic registration = servletContext.addServlet("app", servlet);
        if (registration != null) {
            registration.setLoadOnStartup(1);
            registration.addMapping("/*");
        }
    }
}
